<?php

namespace app\service\admin;

use app\service\base\BaseModel;
use think\facade\Db;

class AdminModel extends BaseModel
{
    protected $name = 'admin';

    public function getPage($arrWhere, $limit)
    {
        return Db::name($this->name)
            ->field('id, username, role_id, real_name, mobile, create_dt')
            ->where($arrWhere)
            ->paginate($limit)
            ->toArray();
    }

    public function getRow($arrWhere)
    {
        return Db::name($this->name)->where($arrWhere)->find();
    }

    public function getList($arrWhere, $field = '')
    {
        return Db::name($this->name)->field($field)->where($arrWhere)->select()->toArray();
    }

    public function add($username, $password, $roleId, $realName, $mobile, $status)
    {
        $arrData               = [];
        $arrData['username']   = $username;
        $arrData['password']   = md5($password);
        $arrData['role_id']    = $roleId;
        $arrData['real_name']  = $realName;
        $arrData['mobile']     = $mobile;
        $arrData['status']     = $status;
        $arrData['create_dt'] = date('Y-m-d H:i:s');
        $arrData['update_dt'] = date('Y-m-d H:i:s');
        return Db::name($this->name)->insertGetId($arrData);
    }

    public function edit($id, $username, $password, $roleId, $realName, $mobile, $status)
    {
        $arrData               = [];
        $arrData['username']   = $username;
        $arrData['role_id']    = $roleId;
        $arrData['real_name']  = $realName;
        $arrData['mobile']     = $mobile;
        $arrData['status']     = $status;
        $arrData['update_dt'] = date('Y-m-d H:i:s');
        if ($password) {
            $arrData['password'] = md5($password);
        }

        return Db::name($this->name)->where('id', $id)->update($arrData);
    }

    public function delById($id)
    {
        return Db::name($this->name)->where('id', $id)->delete();
    }

    public function modifyPassword($id, $password)
    {
        return Db::name($this->name)->where('id', $id)->update(['password' => md5($password)]);
    }
}